using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Data.SqlClient;
namespace Test {
	class Ferrum {
		static void MainC() {
			CultureInfo culture=(CultureInfo)CultureInfo.CurrentCulture.Clone();
			culture.DateTimeFormat.ShortDatePattern="yyyy-MM-dd";
			culture.DateTimeFormat.LongTimePattern="HH:mm:ss";
			Thread.CurrentThread.CurrentCulture=culture;

			System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
			int rows=0;
			Settings set=new Settings("fer");
			using(SqlConnection con=new SqlConnection(set["cs"]))
			using(SqlCommand cmd=new SqlCommand(set["qryb"],con))
			{
				con.Open();
				using(SqlDataReader rdr=cmd.ExecuteReader())
				{
					//FileStream fs=File.Create("data00.csv");
					using(StreamWriter wr=new StreamWriter("data01.csv"))
					{
						while(rdr.Read())
						{
							object[] arrO=new object[rdr.FieldCount];
							string[] arrS=new string[rdr.FieldCount];
							rdr.GetValues(arrO);
							int cnt=0;
							foreach(object obj in arrO)
							{
								if(obj is Byte[])
									arrS[cnt++]=Encoding.ASCII.GetString(obj as Byte[]).Trim();
								else
									arrS[cnt++]=obj.ToString().Trim();
							}
							wr.WriteLine(string.Join("|",arrS));
							rows++;
							if(rows%20000==0)
							{
								Console.WriteLine(rows);
								wr.Flush();
							}
						}
					}
				}
			}
			Console.WriteLine(sw.Elapsed);
			//DAL dal=new DAL(set["cs"]);
			//System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
			//DataTable cu=dal.Query(set["qry"]);
			//Console.WriteLine(sw.Elapsed);
			//File.WriteAllText("cust.csv",DAL.Csv(cu,"|"));
			//DataTable pro=dal.Query("SELECT * FROM master..sysprocesses(nolock)");
		}
	}
}
